﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using T = System.Threading.Tasks;
namespace IceDog.SmallProject.Task
{
    public class NewsPublisher
    {
        public async void StartServiceAsync()
        {
            Console.WriteLine("News Service Start");
            await GetChanelAsync(0);
            await GetChanelAsync(1);
            await GetChanelAsync(2);
            Console.WriteLine("News Service End");
        }

        public void StartService()
        {
            Console.WriteLine("News Service Start");
            T.Task.WaitAll(new T.Task[] { GetChanelAsync(0), GetChanelAsync(1), GetChanelAsync(2) });
            Console.WriteLine("News Service End");
        }

        private async T.Task GetChanelAsync(int chanelId)
        {
            var news = await GetNewsAsync(chanelId);
            foreach (var n in news)
            {
                Console.WriteLine(n);
            }
        }
        private async T.Task<List<string>> GetNewsAsync(int chanelId)
        {
            Random ran = new Random();
            await T.Task.Delay(ran.Next(5000));
            return await T.Task.Factory.StartNew(() => NewsGen(chanelId).ToList());
        }

        private IEnumerable<string> NewsGen(int chanelId)
        {
            var news = new List<string>() { "当前温度是", "当前幸运数字是", "当前人数是" };
            Random ran = new Random();

            for (int i = 0; i < ran.Next(10, 15); i++)
            {
                var aNew = $"{news[chanelId]} {ran.Next(1, 100)}";
                yield return aNew;
            }
        }
    }
}
